/***************************************************************************
 *   Copyright (C) 2009 by Bernhard Neuhofer   *
 *   mail@bneu.at   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
using namespace std;
#include <iostream>
#include "../tools/log.h"

#include "abstractAdFactory.h"
#include "abstractLpFactory.h"
#include "abstractMeFactory.h"
#include "abstractSchedFactory.h"

#include "abstractAdapter.h"
#include "AbstractLp.h"
#include "modelentity.h"
#include "Scheduler.h"

#include <map>
#include <string>

#include "factories.h"

AbstractAdapter* Factories::createAd(string name)
{
  FILE_LOG(logDEBUG)<<"Creating Adapter:"<<name;
	if (_ads[name])
	return _ads[name]->create();
	else return NULL;
}

AbstractLp* Factories::createLp(string name,unsigned int id)
{
	if (_lps[name])
	return _lps[name]->create(id);
	else return NULL;
}

ModelEntity* Factories::createMe(string name,AbstractLp* assocLp,vector<unsigned int> neighbors)
{
	if (_mes[name])
	return _mes[name]->create(assocLp,neighbors);
	else return NULL;
}

Scheduler* Factories::createSched(string name)
{

  if (_sched.find(name)!=_sched.end())
	{
	FILE_LOG(logDEBUG)<<"Creating Scheduler with name:"<<name<<endl;	
	return (_sched[name])->create();
	}
  else 
	{
	FILE_LOG(logERROR)<<"SchedulerClass not found by Name!"<<endl;
	return NULL;
	}
}

void Factories::addAdCreator(AbstractAdFactory* adfac, string name)
{
	FILE_LOG(logDEBUG)<<"Adding Adapter:"<<name;
	_ads[name]=adfac;
}

void Factories::addLpCreator(AbstractLpFactory* lpfac, string name)
{
	_lps[name]=lpfac;
}

void Factories::addMeCreator(AbstractMeFactory* mefac, string name)
{
	_mes[name]=mefac;
}

void Factories::addSchedCreator(AbstractSchedFactory* schedfac,string name)
{
	_sched[name]=schedfac;
	if (!schedfac) FILE_LOG(logDEBUG)<<"no factory"<<endl;
FILE_LOG(logDEBUG)<<"Adding "<<name<<" SchedulerFactory to list. Number of Factories in list:"<<_sched.size()<<endl;
}

void Factories::delAd(AbstractAdapter* ad,string name)
{
if ( _ads[name])
{
 	_ads[name]->destroy(ad);
}

}
void Factories::delLp(AbstractLp* lp,string name)
{
	if(_lps[name])
	_lps[name]->destroy(lp);
}
void Factories::delMe(ModelEntity* me,string name)
{
	if(_mes[name])
	_mes[name]->destroy(me);

}


void Factories::delSched(Scheduler* sched,string name)
{

	if(_sched[name])
	_sched[name]->destroy(sched);
}